package com.sunspring.zdcall.controller.commoncontroller;

import com.sunspring.zdcall.common.SimpleResponse;
import com.sunspring.zdcall.common.constant.enums.DatasourceEnum;
import com.sunspring.zdcall.common.exception.ZdException;
import com.sunspring.zdcall.entity.ZdConfigDatasource;
import com.sunspring.zdcall.param.DataSourceParam;
import com.sunspring.zdcall.quartz.SchedulerManager;
import com.sunspring.zdcall.quartz.entity.RegisterJobEntity;
import com.sunspring.zdcall.quartz.job.RegisterJob;
import com.sunspring.zdcall.service.DatasourceService;
import lombok.extern.slf4j.Slf4j;
import org.quartz.SchedulerException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Objects;

/**
 * @author wulei
 * @date 2018-12-26
 **/
@RestController
@RequestMapping("/sys/datasource")
@Slf4j
public class DatasourceController {

    @Autowired
    private DatasourceService ds;

    @Autowired
    private SchedulerManager schedulerManager;

    /**
     * 查看所有数据源
     */
    @GetMapping("/info")
    public SimpleResponse info() {
        return SimpleResponse.success(ds.getAllDatasource());
    }

    /**
     * 新增数据源
     */
    @PostMapping("/add")
    public SimpleResponse add(@Valid DataSourceParam dsp, BindingResult bindingResult) throws SchedulerException {
        //参数校验
        if (bindingResult.hasErrors()) {
            throw new ZdException(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
        }
        beanCheck(dsp);
        ZdConfigDatasource datasource = new ZdConfigDatasource();
        //非db数据源，不拷贝dbType字段
        if (dsp.getSourceType() != 0) {
            BeanUtils.copyProperties(dsp,datasource,"dbType");
        }else {
            BeanUtils.copyProperties(dsp,datasource);
        }
        ds.addDataSource(datasource);
//        //新增数据源后，配置与之对应的定时任务
//        RegisterJobEntity rje = RegisterJobEntity.build(datasource.getDatasourceId(),datasource.getSourceType());
//        if (dsp.getSourceType() == DatasourceEnum.db.getCode()) {
//            schedulerManager.register(rje, RegisterJob.class);
//        }
        return SimpleResponse.success();
    }

    /**
     * 修改数据源
     * @param datasource ZdConfigDatasource
     */
    @PostMapping("/update")
    public SimpleResponse update(ZdConfigDatasource datasource) {
        ds.updateDatasource(datasource);
        return SimpleResponse.success();
    }

    /**
     * 删除数据源
     * @param datasourceId 数据源id
     */
    @PostMapping("/delete")
    public SimpleResponse delete(int datasourceId) {
        ds.deleteDatasource(datasourceId);
        return SimpleResponse.success();
    }


    /**
     * 数据源参数校验
     * @param datasource DataSourceParam
     */
    private void beanCheck(DataSourceParam datasource) {
        if (datasource.getSourceType() == DatasourceEnum.db.getCode()) {
            if (datasource.getUsername() == null) {
                throw new ZdException("用户名不能为空");
            }else if (datasource.getPassword() == null) {
                throw new ZdException("密码不能为空");
            }
        }
    }
}
